package Interface;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import Usuarios.RepositorioClienteList;
import Usuarios.Usuario;

 
public class JanelaProcuraUsuario extends JInternalFrame implements ActionListener{  
	private JTextField campoCpf;
	private JLabel cpf;
	private JButton buttonProcurar,buttonLimpar;

	
    public JanelaProcuraUsuario () {
    	super("Tela de Procura");
         this.pack();
         JPanel panel = new JPanel();  
         Container container = getContentPane();
         container.add(panel);
         
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
         setLocation(10, 10);  
         setLayout(new GridLayout(6, 1));  
         Container cp = getContentPane();
         panel.setLayout(new GridLayout(4,4)); 
         
         campoCpf = new JTextField();
         cpf = new JLabel("CPF:");
         
         panel.add(cpf);  
         panel.add(campoCpf);
         buttonProcurar = new JButton("Procurar Usuario");
         panel.add(buttonProcurar);
         buttonLimpar = new JButton("Limpar Dados");
         panel.add(buttonLimpar);
         buttonProcurar.addActionListener(this);
         buttonLimpar.addActionListener(this);
         
         cp.add(panel);
         pack();  
         setVisible(true);
     }
    
    public void actionPerformed(ActionEvent e)  {  
    	RepositorioClienteList repositorio = new RepositorioClienteList();
        Usuario a = null;
      
        if(e.getSource() == buttonProcurar){  
            	a = repositorio.procurar(campoCpf.getText());
            	if (a == null) {
            		JOptionPane.showMessageDialog(null, "CPF/Usuario nao encontrado!");
            		setVisible(false);
            	}
            	else { 
            		setVisible(false);
            	    getParent().add( new JanelaAluno(a.getNome(),a.getCpf(),a.getEndereco(),a.getNumero(),a.getDDD(),a.getTelefone()));
            	    
            	}
            
        }else if(e.getSource() == buttonLimpar){    
            campoCpf.setText("");  
            JOptionPane.showMessageDialog(null, "Digite novamente !");
            campoCpf.grabFocus();
     }   
 }
}
